package org.bigjava.mapper;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.bigjava.bean.Garbage;
import org.bigjava.bean.Xiaoqu;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository("garbageMapper")
public interface GarbageMapper {

    //根据垃圾站点编号查询或垃圾站点名称查询信息
    @Select("<script>"+
            "select * from garbage where jsc=1"+
            "<if test='zdbh!=null'> and zdbh = #{zdbh} </if>" +
            "<if test='zname!=null'>and zname = #{zname}</if>"+
            "</script>")
    @Results({
            @Result(property ="xiaoqu",column = "xq_id",javaType = Xiaoqu.class,
                    one = @One(select="org.bigjava.mapper.XiaoquMapper.xiaoQuId",fetchType = FetchType.LAZY))
    })
    public List<Garbage> garbageMoHu(String zdbh, String zname);
    //根据id修改垃圾站点信息
    @Update("update garbage set zdbh=#{zdbh},zname=#{zname},charger=#{charger},phone=#{phone},jtwz=#{jtwz},xq_id=#{xq_id} where id=#{id}")
    public int garbageXiuGai(Garbage garbage);
    //根据id查垃圾站点表
    @Select("select * from garbage where id=#{id}")
    @Results({
            @Result(property ="xiaoqu",column = "xq_id",javaType = Xiaoqu.class,
                    one = @One(select="org.bigjava.mapper.XiaoquMapper.xiaoQuId",fetchType = FetchType.LAZY))
    })
    public Garbage garbageId(int id);
    //根据id假删除
    @Update("update garbage set jsc=0 where id=#{id}")
    public int garbageShanChu(int id);
    //垃圾站点总数
    @Select("select count(*) from garbage where jsc=1")
    public int garbageZong();
    //垃圾站点分页查询
    @Select("select * from garbage where jsc=1 limit #{offset},#{length}")
    @Results({
            @Result(property ="xiaoqu",column = "xq_id",javaType = Xiaoqu.class,
                    one = @One(select="org.bigjava.mapper.XiaoquMapper.xiaoQuId",fetchType = FetchType.LAZY))
    })
    public List<Garbage> garbageFenYe(int offset , int length);
    //添加垃圾站点
    @Insert("insert into garbage(zdbh,zname,charger,phone,jtwz,xq_id)" +
            " values (#{zdbh},#{zname},#{charger},#{phone},#{jtwz},#{xq_id})")
    public int garbageAdd(Garbage garbage);
    //全查出垃圾站点
    @Select("select * from garbage where jsc=1")
    public List<Garbage> garbageQuan();
}
